<?php
/**
 * This is the template for generating CRUD search class of the specified model.
 */

use yii\helpers\StringHelper;


/* @var $this yii\web\View */
/* @var $generator backend\modules\gii\generators\crud\Generator */

$modelClass = StringHelper::basename($generator->modelClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);
if ($modelClass === $searchModelClass) {
    $modelAlias = $modelClass . 'Model';
}
$rules = $generator->generateSearchRules();
$labels = $generator->generateSearchLabels();
$searchAttributes = $generator->getSearchAttributes();
$searchConditions = $generator->generateSearchConditions();

echo "<?php\n";
?>

namespace <?= StringHelper::dirname(ltrim($generator->searchModelClass, '\\')) ?>;

use common\widgets\DateTimePicker;
use common\widgets\Form;
use common\widgets\grid\CheckboxColumn;
use common\widgets\GridView;
use common\widgets\pages\GridViewPage;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use <?= ltrim($generator->modelClass, '\\') . (isset($modelAlias) ? " as $modelAlias" : "") ?>;

/**
 * <?= $searchModelClass ?> represents the model behind the search form about `<?= $generator->modelClass ?>`.
 */
class <?= $searchModelClass ?> extends <?= isset($modelAlias) ? $modelAlias : $modelClass ?>

{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            <?= implode(",\n            ", $rules) ?>,
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * @return string
     */
    public function classLabelName()
    {
        return <?php echo $generator->generateString($modelClass) ?>;
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = <?= isset($modelAlias) ? $modelAlias : $modelClass ?>::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if ( ! $this->validate())
        {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        <?= implode("\n        ", $searchConditions) ?>

        return $dataProvider;
    }

    /**
     * 返回字段的定义信息
     *
     * @return array
     */
    public function gridViewColumns()
    {
        return [
            [
                'class' => CheckboxColumn::className()
            ],
            <?php foreach ($generator->getColumnNames() as $name) { ?>
                <?php

                echo "\n";

                if ($name == 'id')
                {
                    ?>
            [
                'attribute' => '<?php echo $name ?>',
            ],
                    <?php
                }
                elseif ($name == 'created_at' || $name == 'updated_at')
                {
                    ?>
            [
                'attribute' => '<?php echo $name ?>',
                'format'    => ['date', 'php:Y-m-d H:i:s'],
                'filterType' => GridView::FILTER_DATETIME,
            ],
                    <?php
                }
                elseif ($name == 'password')
                {
                }
                else
                {
                    ?>
            '<?php echo $name ?>',
                    <?php
                }
                ?>
            <?php } echo "\n"; ?>
            [
                'class' => 'common\widgets\grid\ActionColumn',
            ],
        ];
    }
}
